<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * @name Company_model
 * @type Model
 * @autor StackFeed LLC
 * @version 2.3
 *
 * @description - Company model which will have all basic
 * database calls to grab full company information and verify
 * values are valid.
 */
class Company_model extends CI_Model 
{
	/*
	 * Default constructor.
	 */
	function __construct()
	{
		parent::__construct();
	}
	
	/*
	 * Get all of the active users in the company.
	 */
	function get_all_users($company_id=1)
	{
		$sql = "SELECT u.id,
					   u.first_name,
					   u.last_name,
					   u.email
			  	  FROM users u
			 	 WHERE u.user_company_id = 1
			   	   AND u.active = 1
			  ORDER BY u.first_name ASC; ";
		
		$query = $this->db->query($sql);
		
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
		
		return $query->result_array();
	}
	
	/*
	 * Get all users including inactive users.
	 */
	function get_all_users_include_inactive($company_id=1)
	{
		$sql = "SELECT u.id,
					   u.first_name,
					   u.last_name,
					   u.email
			  	  FROM users u
			 	 WHERE u.user_company_id = {$company_id}
			  ORDER BY u.first_name ASC; ";
		
		$query = $this->db->query($sql);
		
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
		
		return $query->result_array();
	}
	
	/*
	 * Get all of the active departments in the company.
	 */
	function get_all_departments($company_id=1)
	{
		$sql = "SELECT d.department_id,
					   d.department_name
		  		  FROM department d
		 		 WHERE d.department_company_id = {$company_id}
		   		   AND d.department_is_active = 1
			  ORDER BY d.department_name ASC; ";
		
		$query = $this->db->query($sql);
		
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
		
		return $query->result_array();
	}
	
	/*
	 * Get all of the active projects in the company.
	 */
	function get_all_projects($company_id=1)
	{
		$sql = "SELECT p.project_name,
					   p.project_id,
					   p.project_percentage
		  		  FROM project p
		 		 WHERE p.project_company_id = {$company_id}
		   		   AND p.project_is_active = 1
			  ORDER BY p.project_name ASC;";
		
		$query = $this->db->query($sql);
		
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
		
		return $query->result_array();
	}
	
	/*
	 * Get all of the projects in the company including inactive.
	 */
	function get_all_projects_include_inactive($company_id=1)
	{
		$sql = "SELECT p.project_name,
					   p.project_id
				  FROM project p
				 WHERE p.project_company_id = {$company_id}
			  ORDER BY p.project_name ASC;";
	
		$query = $this->db->query($sql);
	
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
	
		return $query->result_array();
	}
	
	/*
	 * Get all of the active processes in the company.
	 */
	function get_all_processes($company_id=1)
	{
		$sql ="SELECT p.process_id,
					  p.process_name,
					  p.process_is_active
		  	     FROM process p
		 		WHERE p.process_company_id = {$company_id}
			 	  AND p.process_is_active = 1;";
		
		$query = $this->db->query($sql);
		
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
		
		return $query->result_array();
	}
	
	/*
	 * Get all of the processes in a company including inactive.
	 */
	function get_all_processes_include_inactive($company_id=1)
	{
		$sql ="SELECT p.process_id,
					  p.process_name,
					  p.process_is_active
				 FROM process p
				WHERE p.process_company_id = {$company_id};";
		
		$query = $this->db->query($sql);
		
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
		
		return $query->result_array();
	}
	
	/*
	 * Check to see if the ticket is valid for a company.
	 */
	function is_valid_ticket($company_id, $ticket_id)
	{
		$sql = "SELECT t.tickets_id
				  FROM tickets t
				 WHERE t.tickets_id = {$ticket_id}
				   AND t.tickets_company_id = {$company_id};";
		
		$query = $this->db->query($sql);
		
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
		
		return true;
	}
	
	/*
	 * Check to see if the department is valid for a company.
	 */
	function is_valid_department($company_id, $department_id)
	{
		$sql = "SELECT d.department_id
				  FROM department d
			     WHERE d.department_id = {$department_id}
			       AND d.department_company_id = {$company_id};";
		
		$query = $this->db->query($sql);
		
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
		
		return true;
	}
	
	/*
	 * Check to see if the project is valid for a company.
	 */
	function is_valid_project($company_id, $project_id)
	{
		$sql = "SELECT p.project_id
			      FROM project p
			     WHERE p.project_id = {$project_id}
			       AND p.project_company_id = {$company_id};";
		
		$query = $this->db->query($sql);
		
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
		
		return true;
	}
	
	/*
	 * Check to see if the process is valid for a company.
	 */
	function is_valid_process($company_id, $process_id)
	{
		$sql = "SELECT p.process_id
			      FROM process p
			     WHERE p.process_id = {$process_id}
			       AND p.process_company_id = {$company_id};";
		
		$query = $this->db->query($sql);
		
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
		
		return true;
	}
	
	/*
	 * Check to see if the user is valid for a company.
	 */
	function is_valid_user($company_id, $user_id)
	{
		$sql = "SELECT u.id
			      FROM users u
			     WHERE u.id = {$user_id}
			       AND u.user_company_id = {$company_id};";
		
		$query = $this->db->query($sql);
		
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
		
		return true;
	}
	
	/*
	 * Check to see if the ticket file that the user is viewing is valid.
	 */
	function is_valid_ticket_file($company_id, $file_id)
	{
		$sql = "SELECT t.tickets_file_id
				  FROM tickets_files t
				 WHERE t.tickets_file_company_id = {$company_id}
				   AND t.tickets_file_id = {$file_id};";
		
		$query = $this->db->query($sql);
		
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
		
		return true;
	}
	
	/*
	 * Check to see if the department file that the user is viewing is valid.
	 */
	function is_valid_department_file($company_id, $file_id)
	{
		$sql = "SELECT d.departments_files_id
				  FROM departments_files d
				 WHERE d.departments_files_company_id = {$company_id}
				   AND d.departments_files_id = {$file_id};";
	
		$query = $this->db->query($sql);
	
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
	
		return true;
	}
	
	/*
	 * Check to see if the project file that the user is viewing is valid.
	 */
	function is_valid_project_file($company_id, $file_id)
	{
		$sql = "SELECT p.projects_files_id
				  FROM projects_files p
				 WHERE p.projects_files_company_id = {$company_id}
				   AND p.projects_files_id = {$file_id};";
	
		$query = $this->db->query($sql);
	
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
	
		return true;
	}
	
	/*
	 * Check to see if the process file that user is viewing is valid.
	 */
	function is_valid_process_file($company_id, $file_id)
	{
		$sql = "SELECT p.processes_files_id
				  FROM processes_files p
				 WHERE p.processes_files_company_id = {$company_id}
				   AND p.processes_files_id = {$file_id};";
		
		$query = $this->db->query($sql);
		
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
		
		return true;
	}
	
	/*
	 * Assign a user to the default department.
	 */
	function assign_default_department_to_user($company_id, $user_id)
	{
		$sql = "UPDATE users
				   SET user_department_id = (SELECT company_default_department_id 
				   							   FROM company
				   							  WHERE company_id = {$company_id})
				 WHERE id = {$user_id}
			       AND user_company_id = {$company_id};";
		
		return $this->db->simple_query($sql);
	}
	
	/*
	 * Assign a default department to a project.
	 */
	function assign_default_department_to_project($company_id, $project_id)
	{
		$sql = "UPDATE project
				   SET project_department_id = (SELECT company_default_department_id
											      FROM company
											     WHERE company_id = {$company_id})
				 WHERE project_id = {$project_id}
				   AND project_company_id = {$company_id};";
		
		return $this->db->simple_query($sql);
	}
	
	/*
	 * Assign a default project to process.
	 */
	function assign_default_project_to_process($company_id, $process_id)
	{
		$sql = "UPDATE process
				   SET process_project_id = (SELECT company_default_project_id
											   FROM company
											  WHERE company_id = {$company_id})
				 WHERE process_id = {$process_id}
				   AND process_company_id = {$company_id};";
		
		return $this->db->simple_query($sql);
	}
	
	/*
	 * Is default department.
	 */
	function is_default_department($company_id, $department_id)
	{
		$sql = "SELECT department_is_default
				  FROM department
				 WHERE department_id = {$department_id}
				   AND department_company_id = {$company_id};";
		
		$query = $this->db->query($sql);
		$query = $query->result_array();
		
		// If nothing was returned invalid query.
	    if(!$query[0]['department_is_default'])
	    	return false;
				   
		return true;
	}
	
	/*
	 * Is default project.
	 */
	function is_default_project($company_id, $project_id)
	{
		$sql = "SELECT project_is_default
			      FROM project
			     WHERE project_company_id = {$company_id}
			       AND project_id = {$project_id};";
		
		$query = $this->db->query($sql);
		$query = $query->result_array();
		
		// If nothing was returned invalid query.
	    if(!$query[0]['project_is_default'])
	    	return false;
				   
		return true;
	}
	
	/*
	 * Is default process.
	 */
	function is_default_process($company_id, $process_id)
	{
		$sql = "SELECT process_is_default
				  FROM process
				 WHERE process_company_id = {$company_id}
				   AND process_id = {$process_id};";
		
		$query = $this->db->query($sql);
		$query = $query->result_array();
		
		// If nothing was returned invalid query.
		if(!$query[0]['process_is_default'])
			return false;
			
		return true;
	}
	
	/*
	 * Is default user.
	 */
	function is_default_user($company_id, $user_id)
	{
		$sql = "SELECT user_is_default
				  FROM users
				 WHERE user_id = {$user_id}
				   AND user_company_id = {$company_id};";
	
		$query = $this->db->query($sql);
		$query = $query->result_array();
	
		// If nothing was returned invalid query.
		if(!$query[0]['user_is_default'])
			return false;
					
		return true;
	}
	
	/*
	 * Get all the company defaults.
	 */
	function get_company_defaults($company_id=1)
	{
		$sql = "SELECT company_default_department_id,
					   company_default_project_id,
				 	   company_default_process_id
				  FROM company
				 WHERE id = {$company_id};";

		$query = $this->db->query($sql);
		
		// If nothing was returned invalid query.
		if($query->num_rows() == 0)
			return false;
		
		return $query->result_array();
	}
}